/*
 * @Description:
 * @Date: 2024-07-31 08:34:24
 * @LastEditTime: 2024-09-18 14:03:51
 */
import { useUserStore } from '@/store'
import { getResidentRecord, getGridMember } from '@/service/mine/index'
import { useToast } from 'wot-design-uni'
const userStore = useUserStore()
const toast = useToast()
const residentType = computed(() => userStore.userInfo.type)
const residentId = computed(() => userStore.userInfo.residentId)
const gridMemberId = computed(() => userStore.userInfo.gridMemberId)
const accessToken = computed(() => userStore.userInfo.accessToken)
export function useIsLogined() {
  async function updateStatus() {
    try {
      let auditStatus = 0

      if (residentType.value === 'tourist') {
        return
      }
      if (residentType.value === 'cadre') {
        const GridNumber = await getGridMember(gridMemberId.value)
        auditStatus = (GridNumber.data as any).auditStatus
        userStore.setIsLogined(auditStatus)
      } else if (residentType.value === 'villager') {
        const ResidentRecord = await getResidentRecord(residentId.value)
        auditStatus = (ResidentRecord.data as any).auditStatus
        userStore.setIsLogined(auditStatus)
      }

      if (auditStatus === 2 && !accessToken.value) {
        toast.success({
          msg: '审核通过请重新登录！',
        })
        setTimeout(() => {
          uni.reLaunch({ url: '/pages/Login/index' })
          toast.close()
        }, 2000)
      }
    } catch (err) {
      userStore.clearUserInfo()
    }
  }
  return { updateStatus }
}
